home *** CD-ROM | disk | FTP | other *** search
- #!/bin/perl
- $ENV{'PATH'} = "/usr/bsd:/bin/:/usr/bin:/usr/sbin:/usr/bin/X11";
-
- #
- # get and set necessary locations and variables
- #
-
- $userHome = $ENV{"HOME"} ;
- if ($userHome eq "/") {
- $userHome = "";
- }
- $DT_utilities = $ENV{"DT_utilities"} ;
- chdir($DT_utilities) || die "unable to cd to $DT_utilities\n";
- $DT_xconfirm = "$DT_utilities/DT_xconfirm";
- $prefFile = ".netscape-preferences" ;
- $success = 1;
- $failure = 0;
- $f = "$DT_utilities/.DT_Version";
- (-f $f ? `cat $f` : 0 ) =~ /(\d+)/;
- $version = $1;
- $DT_head_pattern = "#-- DT_utilities Compliant File ";
- $DT_end_marker = "#-- End of all DT_utilities stuff ----";
-
- sub path_report {
- local($msg, $path) = @_;
- local($sh_path);
- $sh_path = $path =~ /^$userHome\/(.*)/ ? "\$HOME/$1" : $path;
- print STDERR "$msg ($sh_path)\n";
- }
-
- #
- # routine to read preference file
- #
-
- sub read_prefFile {
- while (<PREF>) {
- next if /^#/;
- next if /^[ ]*$/;
- next unless /^([^:]*):[ \t]*(.*)[ \t\n]*$/;
- $prefs{$1} = $2;
-
- }
- close(PREF);
- }
-
- #
- # routine to write preference file
- #
-
- sub write_prefFile {
- while (($key,$value) = each %prefs) {
- print PREF "$key: $value\n";
- }
- close(PREF);
- }
-
- #
- # routines to read/delete/append/write mimetype and mailcap files
- #
-
- $same = 0;
- $add = 1;
- $delete = 2;
- $changed = 3;
-
- sub read_file {
- local($filename, $dflt_filename, $file_type) = @_;
- $xfilename = $filename;
- $xfile_type = $file_type;
- $file_change = $same;
- $do_ask = 1;
- if (!open(IN, "$filename")) {
- open(IN, "$dflt_filename") ||
- die "Unable to open $dflt_filename\n";
- $file_change = $file_change | $add;
- $do_ask = 0;
- &path_report("Creating $file_type file", $filename);
- }
- @file = <IN>;
- close(IN);
- }
-
- sub delete_stuff {
- local($front, $back) = @_;
- local($i, $j, $num_del);
- for ($i=0; $i <= $#file; ++$i) {last if $file[$i] =~ /^$front/;}
- return ($failure) unless $i <= $#file;
- while ($file[$i] =~ /^#/) {
- $file_change = $file_change | $delete;
- return ($success) if splice(@file, $i, 1) =~ /^$back/;
- }
- for ($j=$i; $j <= $#file; ++$j) {last if $file[$j] =~ /^$back/;}
- return ($success) unless $j <= $#file;
- splice(@file, $i, $j - $i + 1);
- $file_change = $file_change | $delete;
- return ($success);
- }
-
- sub get_file_version {
- for (@file) {
- next unless /^$DT_head_pattern V. (\d+)/;
- return($1);
- }
- return(0);
- }
-
- sub append_stuff {
- local($filename, $description) = @_;
- return($failure) unless open(IN, "$filename");
- push(@file, "#\n#-- DT_utilities - $description ----\n#\n");
- $file_change = $file_change | $add;
- push(@file, <IN>);
- close(IN);
- return($success);
- }
-
- sub write_file {
- local($do_backup, $act, $to, $msg, $f);
- return($success) unless $file_change;
- $f = "${xfilename}.bak";
- if (! -f $xfilename) {
- $do_backup = 0;
- undef $backup;
- } elsif (-f $f) {
- $do_backup = 0;
- $backup = "$f exists so no backup will be done.\n\n";
- } else {
- $do_backup = 1;
- $backup = "The original file will be also saved as:\n";
- $backup .= "${f}.\n\n";
- }
- if ($file_change == $add) {
- $act = "add";
- $to = "to";
- } elsif ($file_change == $delete) {
- $act = "delete";
- $to = "from";
- } else {
- $act = "change";
- $to = "of";
- }
- if ($do_ask) {
- $msg = "In order for Toolbox related tasks to work properly, ";
- $msg .= "it is necessary to $act a few lines $to:\n";
- $msg .= "${xfilename}.\n\n$backup";
- $msg .= "Should I modify the file?\n\n";
- $msg .= "Note: Without these $xfile_type entries, ";
- $msg .= "certain things will likely not work as intended.";
- $reply = `$DT_xconfirm yesno "$msg"`;
-
- chop($reply);
- return($failure) if $reply eq "No";
- }
-
- # save the original just in case
- system("/usr/bin/cp $xfilename ${xfilename}.bak") if $do_backup;
-
- open(OUT, ">$xfilename") || die "Unable to write $xfilename\n";
- for (@file) {print OUT;}
- return($success);
- }
-
- # handle all DT actions to a file
- sub do_file {
- local($filename, $dflt_filename, $file_type, $file) = @_;
- local($f);
-
- &read_file($filename, $dflt_filename, $file_type);
- &delete_stuff("#--FCD96_TOOLBOX_Compliant_File----",
- "#--End of TOOLBOX_SPECIFIC_THINGS");
- &delete_stuff("#--TOOLBOX_Compliant_File----",
- "#--End of TOOLBOX_SPECIFIC_THINGS");
- if (&get_file_version() < $version) {
- &delete_stuff($DT_head_pattern, $DT_end_marker);
- push(@file, "\n$DT_head_pattern V. $version ----\n");
- push(@file,
- "# Do not delete the above line or the end line.\n");
- push(@file, "# DT_utlities uses them to know its stuff.\n");
- &append_stuff($file, "main stuff");
- opendir(DIR, ".") || die "Unable to read current directory\n";
- for $dir (readdir(DIR)) {
- next unless -d $dir;
- next if $dir =~ /^\.{1,2}$/;
- $f = "$dir/$file";
- &append_stuff($f, "$dir helper stuff");
- }
- closedir(DIR);
- push(@file, "#\n$DT_end_marker\n");
- }
- &write_file();
- }
-
-
- #
- # see if the user has a .netscape-preferences file in home dir
- # if not, create one for the user from our template
- #
-
- if ( -e "$userHome/$prefFile") {
- open (PREF, "$userHome/$prefFile");
- &read_prefFile;
- } else {
-
- &path_report("Creating Netscape preference file","$userHome/$prefFile");
- $f = "$DT_utilities/netscape-preferences";
- open(PREF, $f) || die "Template preferences file ($f) missing\n";
- &read_prefFile;
-
- # modify whatever is user specific
- $prefs{"CACHE_DIR"} = "$userHome/.netscape-cache";
- $prefs{"BOOKMARKS_FILE"} = "$userHome/.netscape-bookmarks.html";
- $prefs{"HISTORY_FILE"} = "$userHome/.netscape-history";
- $prefs{"NEWSRC_DIR"} = "$userHome/";
- $prefs{"PERSONAL_MAILCAP"} = "$userHome/.mailcap";
- $prefs{"PERSONAL_MIME_TYPES"} = "$userHome/.mime.types";
- $prefs{"SIGNATURE_FILE"} = "$userHome/.signature";
-
- # and write it out
- $f = "$userHome/$prefFile";
- if (!(open (PREF, ">$f"))) {
- &path_report("Unable to write preferences file", $f);
- exit ($failure);
- }
- &write_prefFile;
- }
-
- #
- # adjust user mimetype file
- #
-
- &do_file($prefs{"PERSONAL_MIME_TYPES"}, "$DT_utilities/mime.types",
- "mime-type", "tb_mime.types");
-
- #
- # adjust user mailcap file
- #
-
- &do_file($prefs{"PERSONAL_MAILCAP"}, "$DT_utilities/mailcap",
- "mailcap", "tb_mailcap");
-
- exit ($success);
-